mini.parse();
var projectName = $("#projectName").val();
var form = new mini.Form("form1");
var orgId = '';
var contractNo = ''; //合同号,用于加载该合同所包含的设备
function SaveData() {
    form.validate();
    if (form.isValid() == false) return;
    var json = GetData();
    CloseWindow(json);
}

////////////////////
//标准方法接口定义
function CheckEventSubTable(data) {
    //跨页面传递的数据对象，克隆后才可以安全使用
    data = mini.clone(data);
    if (data.action == "edit") {
        SetData(data);
    } else if (data.action == "new") {} else if (data.action == "look") {
        SetData(data);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
function SetData(data) {
	var data1 =mini.clone(data);
	form.setData(data1);
}
function CloseWindow(action) {
    console.log(action);
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOkSubTable(e) {
	var type = mini.get("EQ_TYPE_ID").getValue();
	var id = mini.get("EQ_ID").getValue();
	if (type == "") {
        mini.alert("请选择设备类型")
    }
	else if (id == "") {
        mini.alert("请选择设备名称")
    }
	else{
		checkQty();
        SaveData();
	}
}
/* 是否英文+数字 */
function isEnglishAndNumber(v) {

    var re = new RegExp("^[0-9a-zA-Z\_]+$");
    if (re.test(v)) return true;
    return false;
}
function onCancelSubTable(e) {
    CloseWindow("cancel");
}

function labelModel() {
    var fields = form.getFields();
    for (var i = 0,
    l = fields.length; i < l; i++) {
        var c = fields[i];
        if (c.setReadOnly) c.setReadOnly(true); //只读
        if (c.setIsValid) c.setIsValid(true); //去除错误提示
        if (c.addCls) c.addCls("asLabel"); //增加asLabel外观
    }
}
function inputModel() {
    var fields = form.getFields();
    for (var i = 0,
    l = fields.length; i < l; i++) {
        var c = fields[i];
        if (c.setReadOnly) c.setReadOnly(false);
        if (c.removeCls) c.removeCls("asLabel");
    }
    mini.repaint(document.body);
}
function readOnlyForm() {
    $("#button_okSubTable").hide();
    $("#button_onSubTable").hide();
    form.setEnabled(false);
}
/////////////////////////////////////////////////////////////////////
/**设备列表页面**/
function onEquipmentEdit() {
	
    mini.open({
        url: projectName + '/TableDefineAdmin/PurchaseEquipment',
        title: '设备列表',
        width: 800,
        height: 500,
        onload: function() {
            contractNo = mini.get("CONTRACT_NO").getValue();
            var EQ_TYPE_ID = mini.get("EQ_TYPE_ID").getValue();
            var supplierId = mini.get("SUPPLIER_ID").getValue();
            var iframe = this.getIFrameEl();
            var data = {
                business_no: contractNo,
                equipmentType: EQ_TYPE_ID
            };
            iframe.contentWindow.CheckEventEquipment(data);
        },
        ondestroy: function(action) {
            if (typeof(action) == "object" && action.status == 1) {
                var data = action.rows;
                if (data.length > 1) {
                    mini.alert("只能选中一条");
                } else {
                    mini.get("EQ_ID").setValue(data[0].EQ_ID);
                    mini.get("EQ_ID").setText(data[0].EQ_NAME);
                    mini.get("EQ_NAME").setValue(data[0].EQ_NAME);
                    mini.get("EQ_CODE").setValue(data[0].EQ_CODE);
                    mini.get("EQ_TYPE_ID").setValue(data[0].EQ_TYPE_ID);
                    mini.get("EQ_TYPE_ID").setText(data[0].EQ_TYPE_NAME);
                    mini.get("EQ_TYPE_NAME").setValue(data[0].EQ_TYPE_NAME);
                    mini.get("SUPPLIER_ID").setValue(data[0].SUPPLIER_ID);
                    mini.get("SUPPLIER_NAME").setValue(data[0].SUPPLIER_NAME);
                }
            }
        }

    });
}

/**设备类型改变事件*/
function onEqTypeChanged(e) {
    var eq_type_id = this;
    mini.get("EQ_ID").setValue("");
    mini.get("EQ_ID").setText("");
    mini.get("EQ_NAME").setValue("");
    mini.get("EQ_TYPE_NAME").setValue(eq_type_id.getText());
    mini.get("EQ_CODE").setValue("");
//    mini.get("SUPPLIER_ID").setValue("");
    mini.get("EQ_CODE").setValue("");
}

//入库数量的校验 
function checkQty(){
	var qty = mini.get("EQ_NUM").getValue();
	var businessKey = mini.get("CONTRACT_NO").getValue();
	var equipmentId = mini.get("EQ_ID").getValue();
	$.ajax({
		url : projectName+"PurBarcodeAdmin/checkQty",
		async:false,
		cache : false,
		data:{businessKey: businessKey, equipmentId: equipmentId ,qty: qty},
		dataType:'json',
		success : function(data) {
			if (data.status == '0') {
				mini.alert(data.message);
				mini.get("EQ_NUM").setValue("");
				return;
			}
		}
	});
}

